package com.tegtech.api.monitor.controller;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.tegtech.api.monitor.domain.SysUserOnline;
import com.tegtech.api.monitor.service.ISysUserOnlineService;
import com.tegtech.common.constant.CacheKey;
import com.tegtech.common.domain.Result;
import com.tegtech.core.controller.BaseController;
import com.tegtech.core.page.PageResult;
import com.tegtech.core.page.PageSupport;
import com.tegtech.module.auth.domain.LoginUser;
import com.tegtech.starter.log.annotation.Log;
import com.tegtech.starter.log.enums.BusinessType;
import com.tegtech.starter.redis.service.RedisCache;

import cn.hutool.core.lang.Validator;
import cn.hutool.core.util.StrUtil;

/**
 * 在线用户监控
 * 
 * @author Jun
 */
@RestController
@RequestMapping("/monitor/online")
public class SysUserOnlineController extends BaseController {
	
	@Autowired
	private ISysUserOnlineService userOnlineService;

	@Autowired
	private RedisCache redisCache;

	@PreAuthorize("@ss.hasPermi('monitor:online:list')")
	@GetMapping("/list")
	public PageResult list(String ipaddr, String userName) {
		Collection<String> keys = redisCache.keys(CacheKey.LOGIN_TOKEN_KEY + "*");
		List<SysUserOnline> userOnlineList = new ArrayList<SysUserOnline>();
		for (String key : keys) {
			LoginUser user = redisCache.get(key);
			if (Validator.isNotEmpty(ipaddr) && Validator.isNotEmpty(userName)) {
				if (StrUtil.equals(ipaddr, user.getAgent().getIpaddr()) && StrUtil.equals(userName, user.getUsername())) {
					userOnlineList.add(userOnlineService.selectOnlineByInfo(ipaddr, userName, user));
				}
			} else if (Validator.isNotEmpty(ipaddr)) {
				if (StrUtil.equals(ipaddr, user.getAgent().getIpaddr())) {
					userOnlineList.add(userOnlineService.selectOnlineByIpaddr(ipaddr, user));
				}
			} else if (Validator.isNotEmpty(userName) && Validator.isNotNull(user.getUser())) {
				if (StrUtil.equals(userName, user.getUsername())) {
					userOnlineList.add(userOnlineService.selectOnlineByUserName(userName, user));
				}
			} else {
				userOnlineList.add(userOnlineService.loginUserToUserOnline(user));
			}
		}
		Collections.reverse(userOnlineList);
		userOnlineList.removeAll(Collections.singleton(null));
		return PageSupport.buildPageResult(userOnlineList);
	}

	/**
	 * 强退用户
	 */
	@PreAuthorize("@ss.hasPermi('monitor:online:forceLogout')")
	@Log(title = "在线用户", businessType = BusinessType.FORCE)
	@DeleteMapping("/{tokenId}")
	public Result forceLogout(@PathVariable String tokenId) {
		redisCache.del(CacheKey.LOGIN_TOKEN_KEY + tokenId);
		return Result.ok();
	}
}
